package br.com.cefetrn.apoena.view.validator;

import java.util.regex.Pattern;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import br.com.cefetrn.apoena.dominio.entity.Profissional;

import com.opensymphony.xwork.validator.ValidationException;
import com.opensymphony.xwork.validator.validators.FieldValidatorSupport;

public class Validator4FormatoCNPJ  extends FieldValidatorSupport {

	private static final Log LOG = LogFactory.getLog(Validator4FormatoCNPJ.class);
	
	/**
	 * Expressão regular para validação de CNPJ: "##.###.###/####-##" ou
	 * "##############".
	 */
	private static final String REGEX_CNPJ = "(\\d{2}\\.\\d{3}\\.\\d{3}\\/\\d{4}\\-\\d{2})|(\\d{14})";


	public void validate(Object object) throws ValidationException {

		Profissional profissional = (Profissional) getFieldValue(
				"profissional", object);

		if (profissional != null) {

			if (profissional.getCadastro() != null)
				if(!Pattern.matches(REGEX_CNPJ, profissional.getCadastro()))
					addFieldError(getFieldName(), object);

		} else
			LOG.warn("profissional == null");

	}

}
